<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="reports.Report, java.util.*;"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
#pid {width:5em;}
#prodDesc {width:20em;}
#quantity {width:5em;}
td {padding:.25em;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Inventory</title>
</head>
<body>
<a href="/IMS/prodType">Product Type</a>&nbsp;&nbsp;
    <a href="/IMS/product">Product </a>&nbsp;&nbsp;
    <a href="/IMS/searchCustOrder">Search Customer Order</a>&nbsp;&nbsp;
    <a href="/IMS/account.jsp">Update Accounts</a></br></br>
<%
	Cookie[] cookies = request.getCookies();
	Cookie c = null; 
	HashMap<String, String> reportParam = new HashMap<String, String>(); 
	
	for (int i = 0; i < cookies.length; i++) {
		c = cookies[i]; 
		if (reportParam.containsKey(c.getName()) == false) 
			reportParam.put(c.getName(), c.getValue()); 
	}

	String report = reportParam.get("report"); 
	String period = reportParam.get("period"); 
	String startDate = reportParam.get("startDate"); 
	String startMonth = reportParam.get("startMonth");  
	String startYear = reportParam.get("startYear"); 
	String endDate = reportParam.get("endDate"); 
	String endMonth = reportParam.get("endMonth");  
	String endYear = reportParam.get("endYear");  


	if (report.equals("Current Inventory")) {
		Report r = new Report(); 
		//out.println(r.getInventory()); 
	
		HashMap<String, Integer> inventory = r.getInventory(); 
		Set<String> keys = inventory.keySet(); 
		Iterator itr = keys.iterator(); 
		TreeSet<String> pidList = new TreeSet<String>(); 
		
		while (itr.hasNext()) {
			String pid = (String) itr.next(); 
			pidList.add(pid); 
		}
		
		HashMap<String, String> prodNames = r.getProductNames(); 
		
		itr = pidList.iterator(); 
		out.println("<h2>Current Inventory</h2>"); 
		out.println("<table cellpadding='1' border='1'><tr><td>PID</td><td>Product Description</td><td>Quantity</td></tr>"); 
		while (itr.hasNext()) {
			String pid = (String) itr.next(); 
			String color = "style=\'color: black;\'"; 
			if (inventory.get(pid) <= 3) 
				color = "style=\' color: red;\'"; 
				
			out.println("<tr " + color +" ><td id='pid'>" + pid + "</td><td id='prodDesc'>"+ prodNames.get(pid) +"</td><td id='quantity'>" + inventory.get(pid) +"</td></tr>"); 
		}
		out.println("</table>"); 
		
		
		r.closeConnection(); 
	} 
	else {
		Report r = new Report(); 
		//out.println(r.getInventory()); 
	
		HashMap<String, Integer> startingInventory = r.getInventoryByDate(startDate, startMonth, startYear); 
		HashMap<String, Integer> endingInventory = r.getInventoryByDate(endDate, endMonth, endYear); 
		
		//sorts and puts all of the ending inventory pids in ePidList
		Set<String> endingKeys = endingInventory.keySet(); 
		Iterator eItr = endingKeys.iterator(); 
		TreeSet<String> ePidList = new TreeSet<String>(); 
		
		while (eItr.hasNext()) {
			String ePid = (String) eItr.next(); 
			ePidList.add(ePid); 
		}
		
		//sorts and puts all of the starting keys in sPidList
		Set<String> startingKeys = startingInventory.keySet(); 
		Iterator sItr = startingKeys.iterator(); 
		TreeSet<String> sPidList = new TreeSet<String>(); 
		
		while (sItr.hasNext()) {
			String sPid = (String) sItr.next(); 
			sPidList.add(sPid); 
		}
		
		HashMap<String, String> prodNames = r.getProductNames(); 
		
		Iterator itr = ePidList.iterator(); 
		
		out.println("<h2>Inventory Report</h2>");
		out.println("<p>From " + startDate + "-" + startMonth + "-" + startYear + " to " + endDate + "-" + endMonth + "-" + endYear + "</p>"); 
		out.println("<table cellpadding='1' border='1'><tr><td>PID</td><td>Product Description</td><td>Starting Quantity</td><td>Ending Quantity</td><td>Difference</td></tr>"); 

		while (itr.hasNext()) { 
			String eKey = (String) itr.next(); 
			int eQuant = endingInventory.get(eKey); 
			int sQuant = 0; 
			if (startingInventory.containsKey(eKey)) 
				sQuant = startingInventory.get(eKey); 
			int dQuant = eQuant - sQuant; 
			
			String color = "style=\'color: black;\'"; 
			if (endingInventory.get(eKey) <= 3) 
				color = "style=\' color: red;\'"; 
			out.println("<tr " + color +" ><td id='pid'>" + eKey + "</td><td id='prodDesc'>"+ prodNames.get(eKey) +"</td><td id='quantity'>" + sQuant +"</td><td id='quantity'>" + eQuant +"</td><td>" + dQuant +"</td></tr>"); 
		}
		out.println("</table>"); 
		
		r.closeConnection(); 
	}
%>

</body>
</html>